package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.dao.InformationDao;
import com.entity.Cake;
import com.entity.Information;
import com.google.gson.Gson;
import com.servers.CakeServers;
import com.servers.InformationServers;

@Controller
public class InformationController {
	@Autowired
	private InformationServers info;
	@Autowired
	private CakeServers cakeServers;
	@RequestMapping("/information")
	public  String  IntoInfomation(){
		System.out.println("---------进入information--------");
		return  "information";
	}
	@ResponseBody
	@RequestMapping("/getInformation")
	public String  getInfo(){
		System.out.println("---------获取information-JSON-------");
		List<Information> ilist=info.getAllInfomation();
		//封装infomation数据
		Map<String,Object> map1=new HashMap<>();
		map1.put("status", 0);
		//封装listCont下的数组
		List<Map<String,Object>>mlist=new ArrayList<>();
		for(Information information:ilist){
			Map<String,Object> map2=new HashMap<>();
			map2.put("img","images/Informations/"+information.getName()+".png");
			map2.put("name", information.getName());
			map2.put("text",information.getTitle());
			map2.put("data",information.getTime());
			map2.put("infoCont",information.getContent());
			mlist.add(map2);
		}
		map1.put("listCont", mlist);
		Gson gson=new Gson();
		//System.out.println(gson.toJson(map1));
		return gson.toJson(map1);
	}
	/**
	 * GET Information Of Cake Detils
	 * @param name cake name
	 * @return
	 */
	@RequestMapping("/getDetailsInfo")
	public ModelAndView getDetail(String name){
		ModelAndView av=new ModelAndView();
		av.setViewName("detailsInfo");
		System.out.println("name:"+name);
		Cake cake= cakeServers.getCakeByName(name);
		System.out.println("---------"+cake);
		av.addObject("imageInfo", cake.getCakeName());
		av.addObject("price", cake.getCakePrice());
		av.addObject("content", cake.getCakeContext());
		return av;
	}
	public void setInfo(InformationServers info) {
		this.info = info;
	}
	public void setCakeServers(CakeServers cakeServers) {
		this.cakeServers = cakeServers;
	}
	
}
